package com.cubesugar2014.keepup;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;

/**
 * Helper class for providing sample content for user interfaces created by
 * Android template wizards.
 * <p>
 * TODO: Replace all uses of this class before publishing your app.
 */
public class SContent {

    /**
     * An array of sample (dummy) items.
     */
    public static List<SItem> ITEMS = new ArrayList<SItem>();

    /**
     * A map of sample (dummy) items, by ID.
     */
    public static Map<String, SItem> ITEM_MAP = new HashMap<String, SItem>();

    static {
    	/*
    	  int i = 1;
    	  try {
  			
    		  // open the file for reading
    		  InputStream instream = new FileInputStream("./listitem.bin");
    		  
    		  if (instream != null) 
    		  {
    			  InputStreamReader inputreader = new InputStreamReader(instream);
    			  BufferedReader buffreader = new BufferedReader(inputreader);
  
    			  String line;

    			  // read every line of the file into the line-variable, on line at the time
    			  do {
    			     line = buffreader.readLine();
    			    
    			     // do something with the line 
    			     addItem(new SItem(String.valueOf(i), line));
    			     i++;
    			     
    			  } while (line != null);
    		  }
    		    			
  		} catch (FileNotFoundException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		} catch (IOException e) {
  			// TODO Auto-generated catch block
  			e.printStackTrace();
  		}
    	 
    	//default 
    	  
    	if (ITEMS.size() <= 0 )
    	{
	        // Add 3 sample items.
	        addItem(new SItem("1", "Credit Card"));
	        addItem(new SItem("2", "Secret Card"));
	        addItem(new SItem("3", "Extra Image"));
	        
	        
	        FileWriter fOut;
			try {
				
				getApplicationContext().openFileOutput("./listitem.bin",Context.MODE_PRIVATE);
				
				fOut = new FileWriter("./listitem.bin", false);
				fOut.write("Credit Card\n");
				fOut.write("Secret Card\n");
				fOut.write("Extra Image\n");
				
				fOut.close();
			    
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	       
		    
			
	    }
	    */
    	
    }
    public static void addItem(SItem item) {
        ITEMS.add(item);
        ITEM_MAP.put(item.id, item);
    }
    public static void deleteItem(int index) {
         
    	ITEM_MAP.remove(ITEMS.get(index).id);
        ITEMS.remove(index);
         
    }
    
    public static int Count() {
        return ITEMS.size();
    }

    /**
     * A dummy item representing a piece of content.
     */
    public static class SItem {
        public String id;
        public String content;

        public SItem(String id, String content) {
            this.id = id;
            this.content = content;
        }

        @Override
        public String toString() {
            return content;
        }
    }
}
